﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model.Setting;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcGmp
    {
        private NotifyManager _notify;

        public string Title { get; set; } = LanguageManager.GetLanguageValue("GMPSetting");

        public string PermissionCode { get; set; } = "Per_GmpSetting";

        public UcGmp()
        {
            InitializeComponent();
        }

        private void UcGmp_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);

            _triggerButtonsStatus(false);
            _triggerButtonsStatus(true);

            var gi = ConfigAndSettings.GetGmpInfo();
            CbEnableEs.IsChecked = gi.Sign;
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);

            var gi = new GmpInfo { Sign = CbEnableEs.IsChecked ?? false };
            try
            {
                ConfigAndSettings.SetGmpInfo(gi);
                var success = LanguageManager.GetLanguageValue("ActionSuccess");
                _notify.ShowSuccess(success);
                LogRepository.AddLog(LanguageManager.GetLanguageValue("OP-SetGmpInfo"), "Manage");
                _triggerButtonsStatus(true);
            }
            catch (Exception ex)
            {
                _notify.ShowError(ex.Message);
            }

            _triggerButtonsStatus(true);
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
